package com.itheima.apitest.bigdecimaltest;

import java.math.BigDecimal;

public class BigDecimalTest {
    public static void main(String[] args) {
        double a = 10.0;
        double b = 3.0;
        String c = "100";
        //最好用字符串来创建对象 比较准确
        BigDecimal b1 = new BigDecimal(a);
        BigDecimal b2 = new BigDecimal(b);
        BigDecimal b3 = new BigDecimal(c);
        //BigDecimal add() 加法
        System.out.println(b1.add(b2));
        //BigDecimal subtract() 减法
        System.out.println(b1.subtract(b2));
        //BigDecimal multiply() 乘法
        System.out.println(b1.multiply(b2));
        //BigDecimal divide() 除法
        //System.out.println(b1.divide(b2));
        //BigDecimal divide (另一个BigDecimal对象，精确几位，舍入模式)
        //向上取
        BigDecimal result = b1.divide(b2, 3, BigDecimal.ROUND_UP);
        System.out.println(result);
        //向下取
        BigDecimal result2 = b1.divide(b2, 3, BigDecimal.ROUND_FLOOR);
        System.out.println(result2);
        //四舍五入
        BigDecimal result3 = b1.divide(b2, 3, BigDecimal.ROUND_HALF_UP);
        System.out.println(result3);

    }
}
